package TUILayer;
import java.util.Scanner;
import ControlLayer.*;

/**
 * Write a description of class ItemUI here.
 * 
 * @author (dmab0913 Gruppe4) 
 * @version (1.0)
 */

public class ItemUI
{
    private ItemCtr iCtr;// reference til ControlLayer

    /**
     * Constructor for objects of class ItemUI
     */
    public ItemUI()
    {
        iCtr= new ItemCtr();
    }

    public void start()
    {
        ItemMenu();
    }

    private int inputItemId()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Vare nummer:  ");//System.out.println(" indtast Vare nummer:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }           

    private String inputDescription()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" indtast Beskrivelsen: ");
        String description = keyboard.nextLine();
        return description;
    }

    private int inputQuantity()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Antal:  ");//System.out.println(" indtast Antal:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }           

    private double inputPricePrPackage()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" indtast Prisen per pakke: ");
        double pricePrPackage = Double.parseDouble(keyboard.nextLine());
        return pricePrPackage;
    }

    private int inputSizePrPackage()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Størrelse per pakke:  ");//System.out.println(" indtast Størrelse per pakke:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }           

    private double inputPricePrItem()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" indtast Prisen per genstand: ");
        double pricePrItem = Double.parseDouble(keyboard.nextLine());
        return pricePrItem;
    }

    private int inputLocationNumber()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Lokations nummer:  ");//System.out.println(" indtast Lokations nummer:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }           

    private int inputMaxStock()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Maksimal lagerkapacitet:  ");//System.out.println(" indtast Maksimal lagerkapacitet:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }           

    private int inputMinStock()
    {
        // makes an object keyboard to have input from the screen
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Minimal lagerkapacitet:  ");//System.out.println(" indtast Minimal lagerkapacitet:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }           

    private int updateItemMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** opdater Vare Menu ***");
        System.out.println(" (1) Ændre beskrivelse");
        System.out.println(" (2) Ændre kvantitet");
        System.out.println(" (3) Ændre pris per pakke ");
        System.out.println(" (4) Ændre størrelse per pakke");
        System.out.println(" (5) Ændre pris per genstand");
        System.out.println(" (6) Ændre lokationens nummer");
        System.out.println(" (7) Ændre den maksimale lagerkapacitet");
        System.out.println(" (8) Ændre den minimale lagerkapacitet");
        System.out.println(" (9) Return");
        System.out.print("\n foretag et valg: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    private void updateItem()
    {
        boolean exit = false;

        while(!exit)
        {
            int choice = updateItemMenu(); 
            if(choice == 1)
            {   
                iCtr.setDescription(inputItemId(),inputDescription());
            }
            else{
                if(choice == 2)
                {
                    iCtr.setQuantity(inputItemId(), inputQuantity());
                }  
                else{
                    if(choice == 3)
                    {
                        iCtr.setPricePrPackage(inputItemId(), inputPricePrPackage());
                    }  
                    else{
                        if(choice == 4)
                        {
                            iCtr.setSizePrPackage(inputItemId(), inputSizePrPackage());
                        }
                        else{
                            if(choice == 5)
                            {
                                iCtr.setPricePrItem(inputItemId(), inputPricePrItem());
                            }
                            else{
                                if(choice == 6)
                                {
                                    iCtr.setLocationNumber(inputItemId(), inputLocationNumber());
                                }
                                else{
                                    if(choice == 7)
                                    {
                                        iCtr.setMaxStock(inputItemId(), inputMaxStock());
                                    }
                                    else{
                                        if(choice == 8)
                                        {
                                            iCtr.setMinStock(inputItemId(), inputMinStock());
                                        }
                                        else
                                        {
                                            exit = true;
                                        }
                                    }
                                }
                            }
                        }//end else
                    }//end else
                }//end else
            }//end else
        }
    }

    private void ItemMenu()
    {
        boolean exit= false;

        while(!exit)
        {
            int choice = writeItemMenu();
            if(choice == 1)
            {
                createItem();
            }
            else 
            {
                if( choice == 2)
                {
                    //                     showItem();
                }

                else 
                {
                    if(choice == 3)
                    {
                        updateItem();
                    }
                    else 
                    {
                        if(choice == 4)
                        {
                            deleteItem();
                        }
                        else 
                        {
                            if( choice == 5)
                            {
                                //                                 showAllItems();
                                waitForInput();
                            }
                            else
                            {
                                exit=true;
                            }
                        }
                    }
                }
            }
        }
    }

    //     private void showItem()
    //     {
    //         try
    //         {
    //             iCtr.printItem(inputItemId());
    //         }
    //         catch (Exception e)
    //         {
    //             System.out.println("fejl -  prøv igen");            
    //         }
    //     }
    // 
    //     private void showAllItems()
    //     {
    //         iCtr.printAllItems();
    //     }

    private int writeItemMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("***ItemMenu***");
        System.out.println("(1) Opret varen ");
        System.out.println("(2) Find varen ");
        System.out.println("(3) Opdater varen ");
        System.out.println("(4) Slet varen");
        System.out.println("(5) Vis all Varen ");
        System.out.println("(6) Return ");
        System.out.println("\n tag et valg ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void deleteItem()
    {
        iCtr.deleteItem(inputItemId());
    }

    private void createItem()
    { 
        iCtr.createItem(inputItemId(), inputDescription(), inputQuantity(), inputPricePrPackage(), inputSizePrPackage(), inputPricePrItem(), inputLocationNumber(), inputMaxStock(), inputMinStock());
    }

    private void waitForInput()

    {
        Scanner keyboard = new Scanner(System.in);
    }
}

